<?php

namespace Analysis\Model;

use Think\Model;

class ExtenModel extends Model
{
    protected $autoCheckFields = false;
    private $val;

    public function switchToDeal($val)
    {
        $this->val = $val;
        return $this->getDiffType();
    }

    private function getDiffType()
    {
        switch ($this->val[0]) {
            case "12": return $this->serviceTwoOne(8);
                break;
            case "13": return $this->serviceTwoOne(8);
                break;
            case "21": return $this->serviceTwoOne(1);
                break;
            case "23": return $this->serviceThree();
                break;
            case "2301": return $this->serviceThreeAgent();
                break;
            case "2302": return $this->serviceThreeAgent();
                break;
            default: return "系统错误.";
        }
    }

    //21 12 13
    private function serviceTwoOne($st)
    {
        $salestaff = M("salestaff");
        $saleagent = M("saleagent");

        $extenuser = array(
            "ssa_name" => $this->val[1], "ssa_phone" => $this->val[2],
            "ssa_type" => $this->val[0], "ssa_status" => $st,
            'ssa_time' => time()
        );
        $status = $this->getUserExists($this->val[2]);
        //判断是否拥有上级
        $agent = $saleagent->where("sag_phone='" . $this->val[2] . "'")->find();
        $extenuser["ssa_agid"] = $agent["sag_id"];
        $id = $status == null ? $salestaff->add($extenuser) : $status;
        $isbn = $this->insertSaleCode($id);
        if ($isbn != null && $st == 1) {
            return "你好！你的销售码编号是" . $isbn . "，绑定姓名为" . $this->val[1] . "，绑定手机号码为" . $this->val[2];
        }
        if ($isbn != null && $st == 8) {
            return "您的申请我们已经收到，后台审核中。";
        }
        return "申请失败,等待人工审核";
    }

    //23
    private function serviceThree()
    {
        $salestaff = M("salestaff");

        $info = $salestaff->where("ssa_type=23 and ssa_phone='" . $this->val[2] . "' and ssa_status=1")->find();
        if ($info != null) {
            $isbn = $this->insertSaleCode($info["ssa_id"]);
            $suc = "你好！你的销售码编号是" . $isbn . "，绑定姓名为" . $this->val[1] . "，绑定手机号码为" . $this->val[2];
            return $isbn != null ? $suc : "申请失败,等待人工审核";
        } else {
            return "经综合评定，您不符合我们的申请条件，我们无法为您发放优惠码，感谢您对我们的支持。";
        }
    }

    //230102
    private function serviceThreeAgent()
    {
        $saleagent = M("saleagent");
        $salestaff = M("salestaff");

        $where["sag_phone"] = $this->val[2];
        $info = $saleagent->where($where)->find();
        if ($info != null) {
            return "您已经申请过了,无需再次申请。";
        }
        $agentuser = array(
            "sag_name" => $this->val[1], "sag_phone" => $this->val[2],
            "sag_type" => $this->val[0], "sag_status" => 8,
            'sag_time' => time()
        );
        $id = $saleagent->add($agentuser);
        //判断是否拥有下级
        $where["ssa_phone"] = $this->val[2];
        $sav["ssa_agid"] = $id;
        $staff = $salestaff->where($where)->find();
        count($staff) > 0 ? $salestaff->where($where)->save($sav) : null;
        if ($id) {
            return "您的申请我们已经收到，后台审核中。";
        } else {
            return "系统错误。";
        }
    }

    /*
     * 函数包列表
     */

    //写入优惠码
    private function insertSaleCode($id)
    {
        $disisbn = M("disisbn");
        $isbninfo = array(
            "dis_ssaid" => $id,
            "dis_isbn" => $this->val[0] . $this->getDailySort() . strrev(date("ymd")),
            "dis_custype" => $this->val[0],
            "dis_preid" => $this->getRecommendCode($this->val[0]),
            "dis_timecode" => date("ymd"),
            "dis_sn" => $this->getDailySort(),
            "dis_endtimecode" => date("ymd", strtotime("+1 year")),
            "dis_limitcode" => 2,
            "dis_status" => 1,
            "dis_time" => time()
        );
        return $disisbn->add($isbninfo) ? $isbninfo["dis_isbn"] : null;
    }

    //获取当日序号
    private function getDailySort()
    {
        $disisbn = M("disisbn");
        $where["dis_time"] = array("between", array(strtotime(date("Y-m-d")), strtotime(date("Y-m-d 23:59:59"))));
        $count = $disisbn->where($where)->count();
        $ret = $count + 1;
        return $ret < 10 ? "0" . $ret : $ret;
    }

    //获取优惠类型
    private function getRecommendCode($id)
    {
        $saletype = M("saletype");
        $info = $saletype->where("sat_para=" . $id)->find();
        return $info["sat_preid"];
    }

    //检测用户是否存在
    private function getUserExists()
    {
        $salestaff = M("salestaff");
        $where["ssa_phone"] = $this->mob;
        $this->person = $salestaff->where($where)->find();
        return $this->person["ssa_id"] != null ? true : false;
    }
}
